package com.sny.constants;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:yangxupeng
 * @Description:
 * @Date: 2020/7/31 11:09 上午
 * @Modified:
 */
@Data
public class Result<T> {

    private ResultHeader header;
    private Map<String,Object> body;

    public static Result success(String busiCode,String retCode, String retMsg){
        Result result = new Result();
        result.setHeader(result.setResultHeader(busiCode,retCode,retMsg));
        return result;
    }

    public static Result success(String busiCode,String retCode, String retMsg, Object data){
        Result result = new Result();
        result.setHeader(result.setResultHeader(busiCode,retCode,retMsg));
        result.setBody(result.setResultBody(data,busiCode));
        return result;
    }

    @Data
    class ResultHeader{
        private String busiCode;
        private String retCode;
        private String retMsg;
    }

    private ResultHeader setResultHeader(String busiCode,String retCode, String retMsg){
        ResultHeader resultHeader = new ResultHeader();
        resultHeader.setBusiCode(busiCode);
        resultHeader.setRetCode(retCode);
        resultHeader.setRetMsg(retMsg);
        return resultHeader;
    }

    private Map<String,Object> setResultBody(Object data,String busiCode){
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isNotEmpty(busiCode)){
            map.put(busiCode,data);
        }else{
            map.put("data",data);
        }

        return map;
    }
}
